import { Injectable } from '@nestjs/common';
import { CreateCategoryDto } from 'src/dto/create-category.dto';
import { UpdateCategoryDto } from 'src/dto/update-category.dto';
import { Category } from 'src/entity/category.entity';

@Injectable()
export class CategoryService {
  create(createCategoryDto: CreateCategoryDto) {
    return Category.save({
      ...createCategoryDto,
    });
  }

  async findAll(pageNum: number, pageSize: number) {
    const [list, count] = await Category.findAndCount({
      skip: (pageNum - 1) * pageSize,
      take: pageSize,
    });

    return { list, count };
  }

  findOne(id: number) {
    return Category.findOneBy({ id });
  }

  update(id: number, updateCategoryDto: UpdateCategoryDto) {
    return Category.update(id, { ...updateCategoryDto });
  }

  remove(id: number) {
    return Category.update(id, { deleteTime: new Date() });
  }
}
